package com.xuelang.pipeline.app.resource.impl;

import cn.hutool.core.util.IdUtil;
import com.xuelang.jadp.client.sys.dto.UserInfoDTO;
import com.xuelang.pipeline.app.resource.ClusterService;
import com.xuelang.pipeline.app.resource.converter.ClusterDTOToClusterConverter;
import com.xuelang.pipeline.client.common.constant.Constant;
import com.xuelang.pipeline.client.common.exception.BizException;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.ClusterDTO;
import com.xuelang.pipeline.client.resource.dto.ClusterQuery;
import com.xuelang.pipeline.domain.resource.Cluster;
import com.xuelang.pipeline.domain.resource.service.ClusterDomainService;
import com.xuelang.pipeline.domain.resource.service.SpaceDomainService;
import com.xuelang.sys.app.sys.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 集群服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class ClusterServiceImpl implements ClusterService {

    @Autowired
    ClusterDomainService clusterDomainService;
    @Autowired
    SpaceDomainService spaceDomainService;
    @Autowired
    UserManagerService userManagerService;

    private ClusterDTO fullfillCreator(ClusterDTO clusterDTO){
        UserInfoDTO user = userManagerService.getUserById(clusterDTO.getCreatorId());
        if (Objects.nonNull(user)) {
            clusterDTO.setCreator(user.getRealName());
        }else{
            clusterDTO.setCreator(Constant.UNKNOWN_USER);
        }
        return clusterDTO;
    }


    @Override
    public PageRecord<ClusterDTO> pageList(PageParam<ClusterQuery> dtoPageParam) {
        ClusterQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new ClusterQuery() : condition;
        PageRecord<Cluster> pageRecord = clusterDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream()
                        .map(ClusterDTOToClusterConverter.INSTANCE::clusterToClusterDTO)
                        .map(this::fullfillCreator)
                        .collect(Collectors.toList())
        );
    }

    @Override
    public List<ClusterDTO> list(ClusterQuery clusterQuery) {
        return clusterDomainService.list(clusterQuery).stream()
                .map(ClusterDTOToClusterConverter.INSTANCE::clusterToClusterDTO)
                .map(this::fullfillCreator)
                .collect(Collectors.toList()
                );
    }


    @Override
    public Boolean add(ClusterDTO clusterDTO) {
        BizAssert.notNull(clusterDTO.getName(), "name不能为空");
        Cluster cluster = ClusterDTOToClusterConverter.INSTANCE.clusterDTOToCluster(clusterDTO);

        cluster.setUuid(IdUtil.fastSimpleUUID());
        return clusterDomainService.save(cluster);
    }

    @Override
    public Boolean update(ClusterDTO clusterDTO) {
        BizAssert.notNull(clusterDTO.getUuid(), "uuid不能为空");
        BizAssert.notNull(clusterDTO.getName(), "name不能为空");
        Cluster cluster = ClusterDTOToClusterConverter.INSTANCE.clusterDTOToCluster(clusterDTO);
        return clusterDomainService.update(cluster);
    }

    @Override
    public Boolean delete(String clusterUuid) {
        BizAssert.notNull(clusterUuid, "uuid不能为空");
        // 已经被应用下交付空间绑定的集群，不能删除
        if (spaceDomainService.existsSpaceByClusterUuid(clusterUuid)) {
            throw new BizException("该集群已经被交付空间绑定，请先解绑后删除!");
        }
        return clusterDomainService.delete(clusterUuid);
    }


    @Override
    public ClusterDTO query(String clusterUuid) {
        BizAssert.notNull(clusterUuid, "uuid不能为空");
        Cluster cluster = clusterDomainService.query(clusterUuid);
        return ClusterDTOToClusterConverter.INSTANCE.clusterToClusterDTO(cluster);
    }

    @Override
    public Boolean existsSpaceByClusterUuid(String clusterUuid) {
        return spaceDomainService.existsSpaceByClusterUuid(clusterUuid);
    }

}
